<template>
  <div class="app-container">
    <Form v-if="showSearch" v-model:formData="searchInfo" :formItems="formItems">
      <template #footer>
        <div style="text-align: left">
          <el-button size="small" icon="Search" type="primary" @click="goSearch">搜索</el-button>
          <el-button size="small" icon="Refresh" @click="resetSearch">重置</el-button>
        </div>
      </template>
    </Form>
    <h3></h3>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5"> </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="resetSearch"></right-toolbar>
    </el-row>
    <h3>
      <el-button size="small" icon="Plus" type="primary" @click="add" v-hasPermi="['api:czSpec:add']">新增</el-button>
      <!-- <el-button size="small" icon="Plus" type="primary" @click="add"
        >批量删除</el-button
      > -->
      <!-- <el-button
        size="small"
        icon="Edit"
        type="success"
        :disabled="!checkList.length"
        @click="changeStatus(1)"
        v-hasPermi="['api:czSpec:changeStatus']"
        >批量上架</el-button
      >
      <el-button
        size="small"
        icon="Edit"
        type="info"
        :disabled="!checkList.length"
        v-hasPermi="['api:czSpec:changeStatus']"
        @click="changeStatus(2)"
        >批量下架</el-button
      > -->
      <el-button size="small" icon="Edit" type="info" :disabled="!checkList.length"
        v-hasPermi="['api:czSpec:changeMore']" @click="changeMore">批量修改</el-button>
      <el-button type="success" plain icon="Edit" size="small" :disabled="!checkList.length" @click="handlePrice"
        v-hasPermi="['api:czChannelProduct:setPrice']">批量修改渠道价</el-button>
    </h3>
    <x-table :propList="propList" :tableData="info.items" :isLoading="isLoading" isShowExpand isShowCheckBox
      :isBorder="false" @selection-change="selectionChange">
      <template #expand="{ row }">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="收款方式:">
            <span>{{ row.pay?.payment_name || "默认支付" }}</span>
          </el-form-item>
          <el-form-item label="返佣:">
            <template v-if="row.commission">
              <span>{{ row.commission.name }}</span>
              <div style="margin:0 15px;">返佣类型：
                <span v-if="row.commission.mode == 1">销售额百分比</span>
                <span v-if="row.commission.mode == 2">指定金额</span>
                <span v-if="row.commission.mode == 3">纯利润百分比</span>
              </div>
              一级：<span style="color:red;margin-right: 15px;;">{{ row.commission.first_value }}</span>
              二级：<span style="color:red;">{{ row.commission.second_value }}</span>
            </template>
            <template v-else>
              <span>未设置返佣</span>
            </template>
          </el-form-item>
        </el-form>
      </template>

      <!-- <el-table-column label="渠道价" align="center" prop="channel_price">
        <template #default="{ row }">
          <span>{{ row.channel_price }}</span>
        </template>
      </el-table-column> -->
      <template #channel_price="{ row }">
        <span>{{ row.channel_price }}</span>
      </template>
      <template #channel_api_code="{ row }">
        <span>{{ row.channel_api_code }}</span>
      </template>
      <template #status="{ row }">
        <!-- <el-switch
              v-model="value1"
              :active-value="1"
              :inactive-value="2"
              active-text="上架"
              @change="statusChange"
              inactive-text="下架">
            </el-switch> -->
        <el-tag v-if="row.status == 1" type="success" effect="dark">
          是
        </el-tag>
        <el-tag v-else type="danger" effect="dark"> 否 </el-tag>
      </template>
      <template #recharge_type="{ row }">
        <el-tag v-if="row.recharge_type == 1" type="success" effect="dark">
          话费
        </el-tag>
        <el-tag v-else> 流量 </el-tag>
      </template>

      <template #is_stop="{ row }">
        <el-tag type="danger" effect="dark" v-if="row.is_stop == 1">
          是
        </el-tag>
        <el-tag v-else type="success" effect="dark"> 否 </el-tag>
      </template>
      <template #is_limit="{ row }">
        <span v-if="row.is_limit == 1"> 限购 </span>
        <span v-else>不限购</span>
      </template>
      <template #is_auto="{ row }">
        <el-tag type="success" effect="dark" v-if="row.is_auto == 1">
          是
        </el-tag>
        <el-tag type="danger" effect="dark" v-else>否</el-tag>
      </template>
      <template #channel_id="{ row }">
        <span v-if="row.channel_id == -1"> 手动 </span>
        <span v-else>{{ row.channel && row.channel.channel_name }}</span>
      </template>
      <template #stop_fail="{ row }">
        <el-tag type="success" effect="dark" v-if="row.stop_fail == 1">
          是
        </el-tag>
        <el-tag type="danger" effect="dark" v-else>否</el-tag>
      </template>
      <template #is_cache="{ row }">
        <el-tag v-if="row.is_cache == 1" type="danger"> 是 </el-tag>
        <el-tag v-else type="success"> 否 </el-tag>
      </template>
      <template #created_time="{ row }">
        <span>{{ row.created_time }}</span><br />
        <span>{{ row.updated_time }}</span>
      </template>

      <template #op="{ row }">
        <el-button type="primary" link icon="Edit" @click="copy(row)" v-hasPermi="['api:czSpec:add']">复制</el-button>
        <el-button type="primary" link icon="Edit" @click="change(row)"
          v-hasPermi="['api:czSpec:change']">编辑</el-button>
        <el-button type="primary" link icon="Delete" @click="del(row)" v-hasPermi="['api:czSpec:del']">删除</el-button>
      </template>
    </x-table>
    <pagination v-show="info.total > 0" :total="info.total" v-model:page="searchInfo.pageNum"
      v-model:limit="searchInfo.pageSize" @pagination="getInfo" />
    <Dialog width="70%" :title="dialogConfig.title" v-model:isShowDialog="isShowDialog" @confirm="submit"
      :isLoading="dialogLoading">
      <!-- <div>
        <p>{{ curItem.content }}</p>
      </div> -->
      <Form ref="formRef" v-bind="dialogConfig.formConfig">
        <template #detail>
          <!-- <Editor
            v-model:value="dialogConfig.formConfig.formData.detail"
            :height="200"
          ></Editor> -->
          <!-- 保证重新渲染 -->
          <Editor v-if="isShowDialog" :value="richStr" @updateValue="getMsg"></Editor>
        </template>
      </Form>
    </Dialog>
    <!-- 批量修改信息 -->
    <el-dialog title="批量设置" v-model="isMoreChange" width="50vw" append-to-body :close-on-click-modal="false"
      :close-on-press-escape="false">
      <el-table :data="checkList">
        <el-table-column label="产品名称" align="center" prop="cate.name" />
        <el-table-column label="面值" align="center" prop="face_value" />
        <el-table-column label="原价" align="center" prop="market_price" />
        <!-- <el-table-column label="采购价" align="center" prop="user_price" /> -->
        <el-table-column label="默认售价" align="center" prop="sale_price" />
        <el-table-column label="上下架" align="center" prop="status">
          <template #default="{ row }">
            <el-tag v-if="row.status == 1" type="success" effect="dark">
              上架
            </el-tag>
            <el-tag v-else type="danger" effect="dark"> 下架 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否自动" align="center" prop="is_auto">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.is_auto == 1"> 自动 </el-tag>
            <el-tag type="info" v-else>手动</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="渠道" align="center" prop="channel_id">
          <template #default="{ row }">
            <span v-if="row.channel_id == -1"> 手动 </span>
            <span v-else>{{ row.channel && row.channel.channel_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否拦截" align="center" prop="stop_fail">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.stop_fail == 1"> 拦截 </el-tag>
            <el-tag type="info" v-else>不拦截</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否拦截" align="center" prop="is_stop">
          <template #default="{ row }">
            <el-tag v-if="row.is_stop == 1" type="danger"> 停售 </el-tag>
            <el-tag v-else type="success"> 不停售 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否压单" align="center" prop="is_cache">
          <template #default="{ row }">
            <el-tag v-if="row.is_cache == 1" type="danger"> 压单 </el-tag>
            <el-tag v-else type="success"> 不压单 </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <el-form ref="priceForm" :model="moreChangeForm" :rules="moreChangeFormRule" label-width="120px">
        <el-row>
          <!-- <el-col :span="layout">
            <el-form-item label="采购价折扣" prop="user_price_discount">
              <el-input-number
                v-model="moreChangeForm.user_price_discount"
                :precision="2"
                :step="1"
                :min="0"
                :max="100"
                style="width: 250px"
                placeholder="请输入采购价折扣"
              ></el-input-number>
              <span style="margin-left: 30px; color: #f00"
                >注意这里是折扣，不是价格！！！！</span
              >
            </el-form-item>
            <p style="color: #f00">
              采购价 = 原价 * 采购价折扣 / 100，例如：100*92.5/100=92.5
            </p>
          </el-col> -->
          <!-- v-if="userStore.is_show_pay" -->
          <el-col :span="layout">
            <el-form-item label="收款方式" prop="payment_id">
              <el-select v-model="moreChangeForm.payment_id" placeholder="请选择收款方式" filterable clearable>
                <el-option v-for="channel in paymentList" :key="channel.id" :label="channel.payment_name"
                  :value="channel.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="默认售价折扣" prop="sale_price_discount">
              <el-input-number v-model="moreChangeForm.sale_price_discount" :precision="2" :step="1"
                style="width: 250px" :min="0" :max="100" placeholder="请输入默认售价折扣"></el-input-number>
              <span style="margin-left: 30px; color: #f00">注意这里是折扣，不是价格！！！！</span>
            </el-form-item>
            <p style="color: #f00">
              默认售价 = 原价 * 默认售价折扣 / 100，例如：100*92.5/100=92.5
            </p>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="layout">
            <el-form-item label="渠道" prop="channel_id">
              <el-select v-model="moreChangeForm.channel_id" placeholder="请选择渠道" filterable clearable>
                <el-option v-for="channel in channelList" :key="channel.id" :label="channel.channel_name"
                  :value="channel.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="佣金结算规则" prop="commission_id">
              <el-select v-model="moreChangeForm.commission_id" placeholder="请选择" filterable clearable>
                <el-option v-for="item in commissionList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="layout">
            <el-form-item label="是否上下架" prop="status">
              <el-radio-group v-model="moreChangeForm.status">
                <el-radio :label="1" border>上架</el-radio>
                <el-radio :label="2" border>下架</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="是否压单" prop="is_cache">
              <el-radio-group v-model="moreChangeForm.is_cache">
                <el-radio :label="1" border>压单</el-radio>
                <el-radio :label="0" border>不压单</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="layout">
            <el-form-item label="关注下单" prop="is_subscribe">
              <el-radio-group v-model="moreChangeForm.is_subscribe">
                <el-radio :label="1" border>需要关注</el-radio>
                <el-radio :label="2" border>不需要关注</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="是否自动处理" prop="is_auto">
              <el-radio-group v-model="moreChangeForm.is_auto">
                <el-radio :label="1" border>自动处理</el-radio>
                <el-radio :label="0" border>手动处理</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="layout">
            <el-form-item label="是否拦截" prop="stop_fail">
              <el-radio-group v-model="moreChangeForm.stop_fail">
                <el-radio :label="1" border>拦截</el-radio>
                <el-radio :label="0" border>不拦截</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="支付成功文案" prop="pay_success_text">
              <el-input type="textarea" placeholder="请输入支付成功文案" v-model="moreChangeForm.pay_success_text"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="layout">
            <el-form-item label="是否停售" prop="is_stop">
              <el-radio-group v-model="moreChangeForm.is_stop">
                <el-radio :label="1" border>停售</el-radio>
                <el-radio :label="0" border>不停售</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="停售文本" prop="is_stop_text">
              <el-input placeholder="请输入停售文本" v-model="moreChangeForm.is_stop_text"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="layout">
            <el-form-item label="新人价" prop="first_sale_price">
              <el-input-number
                v-model="moreChangeForm.first_sale_price"
                :precision="2"
                :step="1"
                style="width: 250px"
                :min="-100"
                :max="100"
                placeholder="请输入折扣"
              ></el-input-number>
              <span 
                > <b style="color: #f00">（输入示例：93折输入93即可，输入的是折扣）</b></span
              >
            </el-form-item>
          </el-col> 
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="isMoreChange = false">取 消</el-button>
          <el-button type="primary" :loading="isSubmitMoreLoading" @click="submitChangeMore">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog title="批量修改渠道价" v-model="priceOpen" width="50vw" append-to-body :close-on-click-modal="false"
      :close-on-press-escape="false">
      <!-- {{ multiplePriceList[0] }} -->
      <!-- {{ multiplePriceList[0] }} -->
      <el-table :data="multiplePriceList">
        <!-- <el-table-column label="产品名称" align="center" prop="spec" /> -->
        <el-table-column label="产品名称" align="center" prop="cate.name" />
        <el-table-column label="面值" align="center" prop="face_value" />
        <el-table-column label="原价" align="center" prop="market_price" />
        <el-table-column label="渠道价" align="center" prop="channel_price">
          <template #default="{ row }">
            <span>{{ row.channel_price }}</span>
          </template>
        </el-table-column>
        <el-table-column label="当前渠道价折扣(%)" align="center" prop="channel_price_discount">
          <template #default="{ row }">
            <span>{{
              ((row.channel_price / row.market_price) * 100).toFixed(2)
              }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="采购价" align="center" prop="user_price" /> -->
        <el-table-column label="默认售价" align="center" prop="sale_price" />
        <el-table-column label="上下架" align="center" prop="status">
          <template #default="{ row }">

            <el-tag v-if="row.status == 1" type="success" effect="dark">
              上架
            </el-tag>
            <el-tag v-else type="danger" effect="dark"> 下架 </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column label="是否自动" align="center" prop="is_auto">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.is_auto == 1"> 自动 </el-tag>
            <el-tag type="info" v-else>手动</el-tag>
          </template>
        </el-table-column> -->
        <el-table-column label="渠道" align="center" prop="channel_id">
          <template #default="{ row }">
            <span v-if="row.channel_id == -1"> 手动 </span>
            <span v-else>{{ row.channel && row.channel.channel_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否拦截" align="center" prop="stop_fail">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.stop_fail == 1"> 拦截 </el-tag>
            <el-tag type="info" v-else>不拦截</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否拦截" align="center" prop="is_stop">
          <template #default="{ row }">
            <el-tag v-if="row.is_stop == 1" type="danger"> 停售 </el-tag>
            <el-tag v-else type="success"> 不停售 </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否压单" align="center" prop="is_cache">
          <template #default="{ row }">
            <el-tag v-if="row.is_cache == 1" type="danger"> 压单 </el-tag>
            <el-tag v-else type="success"> 不压单 </el-tag>
          </template>
        </el-table-column>
      </el-table>
      <br />
      <!-- {{ priceForm }} -->
      <el-form ref="channelPriceForm" :model="priceForm" label-width="100px">
        <el-form-item label="折扣(原价)" prop="channel_discount">
          <el-input-number v-model="priceForm.channel_discount" :precision="2" :step="1" :min="0" :max="100"
            placeholder="请输入折扣"></el-input-number>
        </el-form-item>
        <p>进价 = 原价 * 折扣 / 100，例如：100*92.5/100=92.5</p>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitPriceForm">确 定</el-button>
          <el-button @click="cancelPrice">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ElLoading } from "element-plus";
import {
  propList,
  formItems,
  dialogConfig,
  sendThis,
  limitTypeChange,
  isLimitChange,
  isNeedCodeChange,
  rechargeTypeChange,
} from "./config";
import {
  getList,
  add,
  change,
  del,
  changeStatus,
  detail,
  changeMore,
} from "@/api/platform/czProductSpec";
import { getAllList } from "@/api/platform/czProductCate";
import { getPayAllList } from "@/api/platform/paySet";

import { getAllList as getCommissionAllList } from "@/api/platform/commissionRule";
import { getArea2 as getArea } from "@/api/common";
import { getAllList as getAllChannel } from "@/api/channel";
import { setMutiplePrice } from "@/api/channel/channelProduct";
import { isNumberStr } from "@/utils/index";
export default {
  name: "platformproductSpecindex",
  data() {
    return {
      multiplePriceList: [],
      priceRules: {
        channel_discount: [
          { required: true, message: "请输入折扣", trigger: "blur" },
        ],
      },
      isSubmitMoreLoading: false,
      priceForm: {},
      priceOpen: false,

      isSubmitMoreLoading: false,
      moreChangeForm: {},
      moreChangeFormRule: {},
      richStr: "",
      detail: "<p>9999</p>",
      curItem: {},
      dialogLoading: false,
      showSearch: true,
      isShowDialog: false,
      dialogConfig,
      propList,
      isLoading: false,
      formItems,
      info: {},
      checkList: [],
      isMoreChange: false,
      channelList: [],
      commissionList: [],
      searchInfo: {
        pageNum: 1,
        pageSize: 10,
        created_time: [],
      },
      layout: 12,
      paymentList: [],
    };
  },
  computed: {},
  created() {
    console.log();
    const screen = window.screen;
    if (screen.width < 500) {
      this.dialogConfig.formConfig.colLayout.span = 24;
      this.layout = 24;
    } else {
      this.dialogConfig.formConfig.colLayout.span = 12;
      this.layout = 12;
    }
    sendThis(this);
    this.init();
  },
  mounted() { },
  methods: {
    init() {
      this.getInfo();
      this.getAllCateList();
      this.getAllChannel();
      this.getArea();
      this.getArea(null, 2);
      this.getCommissionAllList();
      this.getPayAllList()
    },
    async getPayAllList() {
      const { data } = await getPayAllList()
      console.log(data)
      // this.paymentList = data
      // 赋值表单数据
      this.paymentList = [
        {
          payment_name: "默认支付",
          id: -1,
        },
        ...data,
      ];
    },
    async getArea(parent_id, level_type = 1) {
      const params = {
        level_type,
        parent_id,
      };
      const { data } = await getArea(params);
      if (level_type == 1) {
        // 赋值表单省份
        this.dialogConfig.formConfig.formItems.forEach((item) => {
          if (item.prop == "province_id") {
            item.options = data;
          }
          if (item.prop == "limit_province_id") {
            item.options = data;
          }
        });
        // 赋值搜索表单
        this.formItems.forEach((item) => {
          if (item.prop == "province_id") {
            item.options = data;
          }
        });
      }
      if (level_type == 2) {
        // cityList = data;
        this.dialogConfig.formConfig.formItems.forEach((item) => {
          if (item.prop == "city_id") {
            item.options = data;
          }
          if (item.prop == "limit_city_id") {
            item.options = data;
          }
        });
        this.formItems.forEach((item) => {
          if (item.prop == "city_id") {
            item.options = data;
          }
        });
      }
    },
    statusChange(e) {
      console.log(e)

    },
    changeMore() {
      this.moreChangeForm = {};
      this.checkList = JSON.parse(JSON.stringify(this.checkList));
      this.isMoreChange = true;
    },
    async submitChangeMore() {
      const ids = this.checkList.map((item) => item.id);
      const params = {
        ids,
        ...this.moreChangeForm,
      };
      console.log(params);
      this.isSubmitMoreLoading = true;
      try {
        await changeMore(params);
        this.isMoreChange = false;
        this.$message.success("操作成功");
        this.getInfo();
      } finally {
        this.isSubmitMoreLoading = false;
      }
    },
    async getCommissionAllList() {
      // 获取所有的渠道
      // 赋值表单
      const { data } = await getCommissionAllList();
      // channelList.push(...data);
      this.commissionList = data;
      this.dialogConfig.formConfig.formItems.forEach((item) => {
        if (item.prop == "commission_id") {
          item.options = data;
        }
      });
    },
    async getAllChannel() {
      // 获取所有的渠道
      // 1todo 接口获取
      const channelList = [
        {
          id: -1,
          channel_name: "手动",
        },
      ];
      // 赋值表单
      const { data } = await getAllChannel();
      channelList.push(...data);
      this.channelList = channelList;
      this.dialogConfig.formConfig.formItems.forEach((item) => {
        if (item.prop == "channel_id") {
          item.options = channelList;
        }
      });
      this.formItems.forEach((item) => {
        if (item.prop == "channel_id") {
          item.options = channelList;
        }
      });
    },
    submitPriceForm() {
      // console.log("this.$refs.channelPriceForm", this.$refs.channelPriceForm);
      // this.$refs.channelPriceForm.validate((valid) => {
      // if (valid) {
      if (!this.priceForm.channel_discount) {
        this.$modal.msgError("请输入折扣。");
        return false;
      }
      if (!isNumberStr(this.priceForm.channel_discount)) {
        this.$modal.msgError("折扣必须是0-100之间的数字。");
        return false;
      }
      const items = this.checkList.map((item) => {
        return {
          channel_id: item.channel_id,
          // product_id: item.product_id,
          spec_id: item.id,
          channel_price:
            (item.market_price * this.priceForm.channel_discount) / 100,
        };
      });
      setMutiplePrice({ ...this.priceForm, items }).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.priceOpen = false;
        this.getInfo();
      });
    },
    cancelPrice() {
      this.priceOpen = false;
      this.priceForm = {};
    },
    handlePrice() {
      this.priceForm = {};
      this.multiplePriceList = JSON.parse(JSON.stringify(this.checkList));
      this.priceOpen = true;
    },
    getMsg(val) {
      this.richStr = val;

      // this.dialogConfig.formConfig.formData.detail = val;
    },
    async getAllCateList() {
      console.log('getAllCateList')
      const { data } = await getAllList({
        recharge_type: 1
      });
      // 赋值表单数据
      this.dialogConfig.formConfig.formItems.forEach((item) => {
        if (item.prop == "cate_id") {
          item.options = data;
        }
      });
      // 赋值搜索表单
      this.formItems.forEach((item) => {
        if (item.prop == "cate_id") {
          item.options = data;
        }
      });
    },
    async getInfo() {
      this.isLoading = true;
      const params = { ...this.searchInfo };
      params.recharge_type = 1
      const { data } = await getList(params);
      this.info = data;
      this.isLoading = false;
    },
    selectionChange(value) {
      console.log(value);
      this.checkList = value;
    },
    // 批量上下架
    changeStatus(status) {
      const map = {
        1: "上架",
        2: "下架",
      };
      this.$confirm("确定要批量修改为" + map[status] + "吗?", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(async () => {
          // 获取id
          const ids = this.checkList.map((item) => item.id);
          const params = {
            ids,
            status,
          };
          changeStatus(params).then(() => {
            this.getInfo();
            this.message({
              type: "success",
              message: "修改成功!",
            });
          });
        })
        .catch(() => {
          // this.message({
          //   type: "success",
          //   message: "修改失败",
          // });
        });
    },
    resetSearch() {
      this.searchInfo = this.$options.data.call(this).searchInfo;
      this.searchInfo.created_time = [];
      this.getInfo();
    },
    goSearch() {
      // 重置时间
      this.getInfo();
    },
    add() {
      this.dialogConfig.formConfig.formData = {};
      this.richStr = "";
      this.isShowDialog = true;
    },
    // // 限购类型改变时触发
    // limitTypeChange(value, formItems) {
    //   console.log(value, formItems);
    //   // todo显示时间范围选择
    // },
    async submit() {
      console.log(this.dialogConfig.formConfig.formData.detail);
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          const params = { ...this.dialogConfig.formConfig.formData };
          // console.log(params.limit_type_value);
          // 处理限购配置
          params.limit_buy_config = {
            limit_type: params.limit_type,
            limit_value: params.limit_value,
            limit_time: params.limit_time,
            limit_type_value: params.limit_type_value,
          };
          params.detail = this.richStr;
          this.dialogLoading = true;
          try {
            if (this.dialogConfig.formConfig.formData.id) {
              await change(params);
            } else {
              await add(params);
            }
            this.$message.success("操作成功!");
            this.isShowDialog = false;
            this.getInfo();
          } finally {
            this.dialogLoading = false;
          }
        } else {
          this.$message.error("请将数据填写完整");
        }
      });
    },
    copy(item) {
      // 修改
      const loading = ElLoading.service({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      detail(item.id)
        .then((res) => {
          isLimitChange(
            res.data.is_limit,
            this.dialogConfig.formConfig.formItems
          );
          // 处理限购数据
          if (res.data.limit_buy_config) {
            res.data.limit_value = res.data.limit_buy_config.limit_value;
            res.data.limit_type = res.data.limit_buy_config.limit_type;
            res.data.limit_time = res.data.limit_buy_config.limit_time;
            res.data.limit_type_value =
              res.data.limit_buy_config.limit_type_value;
            // 验证表单显示
            if (res.data.is_limit == 1) {
              limitTypeChange(
                res.data.limit_type,
                this.dialogConfig.formConfig.formItems
              );
            }
          }
          res.data.user_price = +res.data.user_price;
          res.data.sale_price = +res.data.sale_price;
          res.data.market_price = +res.data.market_price;

          res.data.province_id = res.data.province_id || undefined;
          res.data.city_id = res.data.city_id || undefined;

          this.dialogConfig.formConfig.formData = res.data;
          this.richStr = res.data.detail;
          this.isShowDialog = true;
          if (res.data.province_id) {
            // this.getArea(res.data.province_id, 2);
          }

          isNeedCodeChange(
            res.data.is_need_code,
            this.dialogConfig.formConfig.formItems
          );
          rechargeTypeChange(
            res.data.recharge_type,
            this.dialogConfig.formConfig.formItems
          );
          delete this.dialogConfig.formConfig.formData.id;
        })
        .finally(() => {
          loading.close();
        });
    },
    change(item) {
      // 修改
      const loading = ElLoading.service({
        lock: true,
        text: "Loading",
        background: "rgba(0, 0, 0, 0.7)",
      });
      detail(item.id)
        .then((res) => {
          isLimitChange(
            res.data.is_limit,
            this.dialogConfig.formConfig.formItems
          );
          // 处理限购数据
          if (res.data.limit_buy_config) {
            res.data.limit_value = res.data.limit_buy_config.limit_value;
            res.data.limit_type = res.data.limit_buy_config.limit_type;
            res.data.limit_time = res.data.limit_buy_config.limit_time;
            res.data.limit_type_value =
              res.data.limit_buy_config.limit_type_value;
            // 验证表单显示
            if (res.data.is_limit == 1) {
              limitTypeChange(
                res.data.limit_type,
                this.dialogConfig.formConfig.formItems
              );
            }
          }
          res.data.user_price = +res.data.user_price;
          res.data.sale_price = +res.data.sale_price;
          res.data.market_price = +res.data.market_price;

          res.data.province_id = res.data.province_id || null;
          res.data.city_id = res.data.city_id || null;

          this.dialogConfig.formConfig.formData = res.data;
          this.richStr = res.data.detail;
          this.isShowDialog = true;
          if (res.data.province_id) {
            // this.getArea(res.data.province_id, 2);
          }

          isNeedCodeChange(
            res.data.is_need_code,
            this.dialogConfig.formConfig.formItems
          );
          rechargeTypeChange(
            res.data.recharge_type,
            this.dialogConfig.formConfig.formItems
          );
        })
        .finally(() => {
          loading.close();
        });
      // this.dialogConfig.formConfig.form
    },
    del(item) {
      // 删除
      this.$confirm("确定要删除吗?", "提示", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(async () => {
          await del(item.id);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getInfo();
        })
        .catch(() => { });
    },
  },
};
</script>

<style lang="scss" scoped></style>
